package org.popupcommunication.model;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
@Table(name = "sujets")
public class SujetForum implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int idSujet;

	@NotNull
	@Size(min = 4, max = 255)
	@Column(nullable = false, length = 255)
	private String titre;

	@NotNull
	@Column(nullable = false, columnDefinition = "TEXT")
	private String contenu;
	
	@ManyToOne
	private User createur;

	@ManyToOne
	private Group forum;

	private Date dateCreation;	
	
	private Date dateModification;
	private Date dateModeration;


	private Date dateLastResponse;	
	
	private int nbResponses;
	
	private boolean isClose;
	
	@OneToMany(mappedBy = "sujet")
	private Set<ResponseForum> listReponses;			

	public Set<ResponseForum> getListReponses() {
		return listReponses;
	}

	public void setListReponses(Set<ResponseForum> rep) {
		this.listReponses = rep;
	}
	
	public void addReponse(ResponseForum r) {
		this.listReponses.add(r);
		this.nbResponses++;
		this.dateLastResponse = new Date();
	}	
	public void deleteReponse(){
		this.nbResponses--;
	}
	
	public void lock(){
		this.isClose = true;
	}
	public void unlock(){
		this.isClose=false;
	}
	public SujetForum(){}
	public SujetForum(User u, Group g){
		this.createur=u;
		this.forum=g;
		this.titre=null;
		this.contenu=null;
	}
	
	public int getIdSujet() {
		return idSujet;
	}

	public void setIdSujet(int idSujet) {
		this.idSujet = idSujet;
	}

	public String getTitre() {
		return titre;
	}

	public void setTitre(String titre) {
		this.titre = titre;
	}

	public String getContenu() {
		return contenu;
	}

	public void setContenu(String contenu) {
		this.contenu = contenu;
	}

	public User getCreateur() {
		return createur;
	}
	public void setCreateur(User createur) {
		this.createur = createur;
	}

	public Group getForum() {
		return forum;
	}

	public void setForum(Group forum) {
		this.forum = forum;
	}

	public Date getDateCreation() {
		return dateCreation;
	}

	public void setDateCreation(Date dateCreation) {
		this.dateCreation = dateCreation;
	}

	public Date getDateLastResponse() {
		return dateLastResponse;
	}

	public void setDateLastResponse(Date dateLastResponse) {
		this.dateLastResponse = dateLastResponse;
	}

	public int getNbResponses() {
		return nbResponses;
	}

	public void setNbResponses(int nbResponses) {
		this.nbResponses = nbResponses;
	}
	
	public boolean getisClose() {
		return isClose;
	}

	public void setClose(boolean isClose) {
		this.isClose = isClose;
	}
	public Date getDateModification() {
		return dateModification;
	}

	public void setDateModification(Date dateModification) {
		this.dateModification = dateModification;
	}

	public Date getDateModeration() {
		return dateModeration;
	}

	public void setDateModeration(Date dateModeration) {
		this.dateModeration = dateModeration;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	@Override
	public String toString() {
		return "SujetForum [idSujet=" + idSujet + ", titre=" + titre
				+ ", contenu=" + contenu + ", createur=" + createur
				+ ", forum=" + forum + ", dateCreation=" + dateCreation
				+ ", dateLastResponse=" + dateLastResponse + ", nbResponses="
				+ nbResponses + ", isClose=" + isClose + ", listReponses="
				+ listReponses + "]";
	}
	
	public void addTagEditSujet(){
		this.dateModification = new Date();
		this.dateLastResponse = this.dateModification;
	}
	public void addTagModereSujet(){
		this.dateModeration = new Date();
		this.dateLastResponse = this.dateModeration;
	}
}
